import './public-path';
import { createApp } from 'vue';
import App from './App.vue';
import 'ant-design-vue/dist/antd.css';
import { setupRouter } from './router';
// import router from './router';
import store from './store';
import '@/assets/styles/base/tailwind.css';
import './assets/appFont/iconfont.css';
import './assets/webFont/iconfont.css';
import './assets/zuHu/iconfont.css';
import '@vant/touch-emulator';
import 'vant/lib/index.css';
import { TjPick, TjSelectDevice } from 'tianjian-vue';
import TjDesign from 'tj-design-vue';
import 'tj-design-vue/lib/index.css';
import 'tianjian-vue/es/pick/index.css';
import 'tianjian-vue/es/selectDevice/index.css';
import { ConfigProvider, Message } from 'ant-design-vue';
import initPlugins from '@/plugins';
import auth from '@/api/utils/auth.js';
import { SvgIcon } from './components/SvgIcon';
const { changeThemeColor } = require('./utils/theme');

let history;
let router;
let app;
let antd;
Message.config({
  duration: 0.6
});
// 用于应用每次初始化时 判断当前是否属于被缓存状态，也就是说子应用只有第一次被加载时才会发送请求解析子应用
// 以后每次切换子应用都是从js的缓存中直接读取
// 除非 micro-app的destroy是true的情况下 每次切换子应用都会重新加载资源进行初始化
let isCached = false;
let appId = 0;

async function mount() {
  const { router: _router, history: _history } = setupRouter();
  history = _history;
  router = _router;
  app = createApp(App);
  auth(app);
  initPlugins(app);
  app.use(TjDesign);
  app.use(store);
  app.use(antd);
  app.use(TjPick);
  app.use(TjSelectDevice);
  app.component('SvgIcon', SvgIcon);
  app.use(router);
  app.mount('#app');
  if (window.__MICRO_APP_ENVIRONMENT__) {
    // 全局监听函数
    window.microApp.addGlobalDataListener(({ data, methods, appName } = {}) => {
      console.log('全局监听', data, methods, appName);
      if (appName === 'microBaseApp') {
        window.$microAppMethods = methods;
        if (data.token) {
          store.commit('user/setTokens', data.token);
          localStorage.setItem('managementToken', data.token);
        }
        if (data.userInfo.identity) {
          localStorage.setItem('USER_INFO', JSON.stringify(data.userInfo));
        }
        // 接受的主题颜色
        if (data.primaryColor) {
          changeThemeColor(data.primaryColor);
          getColor(data.primaryColor);
          store.commit('user/setPrimaryColor', data.primaryColor);
          document.body.style.setProperty('--van-primary-color-web', data.primaryColor);
          localStorage.setItem('domitoryThemeColor', data.primaryColor);
        }
      }
    }, true);
    // 独立的监听函数 监听当前自身（子应用）与基座的通信
    window.microApp.addDataListener(async (data = {}) => {
      localStorage.setItem('USER_INFO_DORMITORY_NEW', JSON.stringify(data));
      console.log(data);
      if (isCached && data.path) {
        router.push(data.path);
      }
      // 新版本
      if (data.appData.applicationId) {
        appId = data.appData.applicationId;
        sessionStorage.setItem('appId', appId);
      }
    }, true);
  } else {
    console.log(3);
    getData();
  }
}

async function getData() {
  let primaryColor = '#6764ed';
  changeThemeColor(primaryColor);
  store.commit('user/setPrimaryColor', primaryColor);
  document.body.style.setProperty('--van-primary-color-web', primaryColor);
  localStorage.setItem('domitoryThemeColor', primaryColor);
  getColor(primaryColor);
  localStorage.setItem('managementToken', 'cf6f0ba8-988e-4a1e-9700-948cd39e0f3a');
  let obj = {
    address: '北京市顺义区',
    age: 24,
    avatar:
      'https://ding-dong-anzhuo-usercent.obs.cn-east-3.myhuaweicloud.com/assets/,2022-03-25/294917804136796160_3128c51d-eabd-4975-b634-551df8dc3372.jpeg',
    balance: 0,
    birthDate: '2018-03-25',
    certification: 0,
    contact: '',
    contactPhone: '',
    description: '',
    email: '',
    id: '294917804136796160',
    idCard: '370124199706283024',
    idCardType: 0,
    identity: 8,
    isAdmin: 1,
    mainOrganizeId: '294918614803300352',
    mainSchoolId: '294918614828466176',
    mobile: '13000001111',
    nation: 0,
    nickName: '',
    placeCode: 0,
    realName: '更好大',
    roleId: '294918615034171392,294923435157688321',
    roleName: '创建人,学科教师',
    sex: 0,
    status: 0,
    username: 'zhoukexindezuzhishujuxinxi1fc57250c7dd4681a948838b141a6067'
  };
  localStorage.setItem('USER_INFO', JSON.stringify(obj));
}

function getColor(data) {
  console.log(data);
  ConfigProvider.config({
    theme: {
      primaryColor: data
    }
  });
}
function unmount() {
  app.unmount();
  history.destroy();
  history = null;
  router = null;
  app = null;
  isCached = true;
  window.microApp.clearGlobalDataListener();
}

function initMicroApp() {
  window[`micro-app-${window.__MICRO_APP_NAME__}`] = {
    mount,
    unmount
  };
}
if (window.__MICRO_APP_ENVIRONMENT__) {
  initMicroApp();
} else {
  mount();
}
